<template>
    <div>
        <el-form label-width="100px ">
            <el-form-item label="SKU名称">
                <el-input type="text" placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
            </el-form-item>
            <el-form-item label="价格(元)">
                <el-input type="number" placeholder="价格(元)" v-model="skuParams.price"></el-input>
            </el-form-item>
            <el-form-item label="重量(克)">
                <el-input type="number" placeholder="重量(克)" v-model="skuParams.weight"></el-input>
            </el-form-item>
            <el-form-item label="sku描述">
                <el-input type="textarea" placeholder="sku描述" v-model="skuParams.skuDesc"></el-input>
            </el-form-item>
            <el-form-item label="平台属性">
                <el-form :inline="true" label-width="100px">
                    <el-form-item v-for="(item, index) in attrArr" :key="item.id" :label="item.attrName">
                        <el-select v-model="item.attrIdAndValueId">
                            <el-option :value="`${item.id}:${item1.id}`" v-for="(item1, index) in item.attrValueList"
                                :key="item1.id" :label="item1.valueName"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </el-form-item>
            <el-form-item label="销售属性">
                <el-form :inline="true">
                    <el-form-item :label="item.saleAttrName" v-for="(item, index) in saleArr" :key="item.id" style="">
                        <el-select v-model="item.saleIdAndValueId">
                            <el-option :value="`${item.id}:${saleAttrValue.id}`"
                                v-for="(saleAttrValue, index) in  item.spuSaleAttrValueList"
                                :label="saleAttrValue.saleAttrValueName" :key="saleAttrValue.id"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </el-form-item>
            <el-form-item label="图片名称">
                <el-table border :data="imgArr" ref="table">
                    <el-table-column type="selection" width="120px" align="center"></el-table-column>
                    <el-table-column label="图片">
                        <template #="{ row, $index }">
                            <img :src="row.imgUrl" alt="" style="width:100px;height:100px">
                        </template>
                    </el-table-column>
                    <el-table-column label="名称" prop="imgName"></el-table-column>
                    <el-table-column label="操作">
                        <template #="{ row, $index }">
                            <el-button type="primary" size="small" @click="handler(row)">设置默认</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" size="default" @click="save">保存</el-button>
                <el-button type="primary" size="default" @click="cancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script setup lang='ts'>
import { ref, reactive } from 'vue'
//引入spu数据类型 
import type { SpuData, SkuData } from '@/api/product/spu/type';
//引入请求API
import { reqAttr } from '@/api/product/attr/index'
//引入获取照片墙
import { reqAllImage, reqSpuHasSaleAttr, reqAddSku } from '@/api/product/spu/index'
import { ElMessage } from 'element-plus';
//平台属性
let attrArr = ref<any>([])
//销售属性的数组
let saleArr = ref<any>([])
//照片墙的数组
let imgArr = ref<any>([])
//获取table组件实例
let table = ref<any>()
//收集Sku的参数
let skuParams = reactive<SkuData>({
    category3Id: '',//三级分类的ID
    spuId: '',//已有的SPU的ID
    tmId: '', //SPU品牌的ID
    skuName: '',//sku名字
    price: '', //sku价格
    weight: '', //sku重量
    skuDesc: '', //sku的描述
    skuAttrValueList: [],//平台属性
    skuSaleAttrValueList: [],//平台销售属性
    skuDefaultImg: '' //sku图片地址
})
//自定义事件的方法
let $emit = defineEmits(['changeSence']);
//取消按钮的回调
const cancel = () => {
    console.log(123)
    $emit('changeSence', { boo: false, params: '' })
}
//子组件暴露的方法
const initSkuData = async (c1Id: number | string, c2Id: number | string, spu: SpuData) => {
    //收集数据
    skuParams.category3Id = spu.category3Id;
    skuParams.spuId = spu.id as number | string;
    skuParams.tmId = spu.tmId;
    //获取平台属性
    let result: any = await reqAttr(c1Id, c2Id, spu.category3Id);
    //获取对应的销售属性
    let result1: any = await reqSpuHasSaleAttr(spu.id as number);
    //获取照片墙的数据
    let result2: any = await reqAllImage(spu.id as number);
    //平台属性
    attrArr.value = result.data;
    //销售属性
    saleArr.value = result1.data;
    //图片
    imgArr.value = result2.data;
}
//设置默认按钮的点击回调
const handler = (row: any) => {
    //点击时清空照片数组的选中状态
    imgArr.value.forEach((item: any) => {
        table.value.toggleRowSelection(item, false)
    })
    //复选框选中
    table.value.toggleRowSelection(row, true);
    // 收集图片地址
    skuParams.skuDefaultImg = row.imgUrl;
}

//保存按钮的方法
const save = async () => {
    //整理数据
    // 平台属性
    let attrArrs = attrArr.value.reduce((prev: any, next: any) => {
        if (next.attrIdAndValueId) {
            let [attrId, valueId] = next.attrIdAndValueId.split(':');
            prev.push({
                attrId,
                valueId
            })
        }
        return prev;
    }, [])
    skuParams.skuAttrValueList = attrArrs;
    //销售属性
    skuParams.skuSaleAttrValueList = saleArr.value.reduce((prev: any, next: any) => {
        if (next.saleIdAndValueId) {
            let [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(':');
            prev.push({ saleAttrId, saleAttrValueId })
        }
        return prev;
    }, [])
    //发起请求 
    let result: any = await reqAddSku(skuParams);
    if (result.code == 200) {
        ElMessage({
            type: 'success',
            message: '添加成功'
        });
        //通知父组件切换场景
        $emit('changeSence', { boo: false, params: '' })
    } else {
        ElMessage({
            type: 'error',
            message: '添加失败'
        })
    }
    console.log(result)
    //请求处理

}

//对外暴露的方法
defineExpose({
    initSkuData
})
</script>
<style lang='scss' scoped></style>